package com.hxzy.controller;

import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.resp.R;
import com.hxzy.feign.OrderServiceFeign;
import com.hxzy.order.req.OrderSearchReq;
import com.hxzy.order.resp.OrderResp;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/10/12 16:11
 * @Version 1.0
 */
@Log4j2
@RestController
@RequestMapping(value = "/mail")
public class MailController {

    /**
     * 调用接口
     */
    @Autowired
    private OrderServiceFeign  orderServiceFeign;

    /**
     * 发邮件
     * @param orderId
     * @return
     */
   @GetMapping(value = "/send/{orderId}")
    public R    mailGetByOrderId(@PathVariable(value = "orderId") Integer  orderId){

//       try (Entry entry = SphU.entry("xxx")) {
//           // 被保护的逻辑
//           System.out.println("hello world");
//       } catch (BlockException ex) {
//           // 处理被流控的逻辑
//           System.out.println("blocked!");
//       }

       //远程访问订单数据
       R<OrderResp> orderRespR = this.orderServiceFeign.orderFindById(orderId);
       //查询成功
       if(orderRespR.getCode() .equals( AckCode.SUCCESS.getCode())){
           //发送邮件
           OrderResp data = orderRespR.getData();
           log.info(data.toString());

           //以下是处理业务逻辑做什么

           return R.okHasData("邮件发送成功");
       }else{
           throw new RuntimeException(orderRespR.getMsg());
       }
   }


    /**
     * 查询订单分页
     * @param req
     * @return
     */
    @GetMapping(value = "/order/page")
    public R<Map<String,Object>>    mailFindOrderPage( OrderSearchReq req){
       // R<Map<String, Object>> mapR = this.orderServiceFeign.orderData(req);

        R<Map<String, Object>> mapR = this.orderServiceFeign.orderDataParam(req.getPage(),req.getSize(),req.getUserId(),req.getName());
        return mapR;
    }
}
